<!--
 * @Author: Li Zengkun
 * @Date: 2022-09-26 17:11:31
 * @LastEditors: Li Zengkun
 * @LastEditTime: 2022-09-26 21:08:14
 * @Description: 
-->
<template>
  <div>
    <el-card shadow="hover">
      <template #header>
        <span>登录</span>
      </template>
      <el-form :model="form" :rules="rules"  label-position="top">
        <el-form-item label="账户">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <p>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </p>
      <div class="bottom">
        <el-button link @click="toRegister">注册账户</el-button>
        <el-button link>忘记密码</el-button>
      </div>
    </el-card>
  </div>
</template>
 
<script setup>
import { ref, reactive } from "vue";
import {useRouter} from "vue-router"
import {admin} from "@/api"

const router = useRouter()

const form = ref({
  username: "",
  password: "",
});

const rules = [{}];

const toRegister = ()=>{
router.push('register')
}

const handleLogin =async ()=>{
   let {status,msg,data} = await admin.login(form.value)

   if(status){
      sessionStorage.id = data.id
      sessionStorage.role = data.role
      sessionStorage.token = data.token
      router.push({name:'GoodsList'})
   }
   ElMessage({
      type:status?'success':'error',
      message:msg
   })

}
</script>

<style lang='less' scoped>
</style>
